// MainActivity.java
package com.example.a001.part25;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.a001.R;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tvMessage, tvContacts, tvFriends, tvMe;
    private ImageView ivMessage, ivContacts, ivFriends, ivMe;
    private Fragment fg1, fg2, fg3, fg4;
    private FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main14);
        bindViews();
        fragmentManager = getSupportFragmentManager();

        // 获取从登录页面传递过来的账号密码
        String username = getIntent().getStringExtra("username");
        String password = getIntent().getStringExtra("password");
        String content = "账号: " + username + "\n密码: " + password;

        setTabSelection(0, content);
    }

    private void bindViews() {
        tvMessage = findViewById(R.id.tv_message);
        tvContacts = findViewById(R.id.tv_contacts);
        tvFriends = findViewById(R.id.tv_friends);
        tvMe = findViewById(R.id.tv_me);

        ivMessage = findViewById(R.id.iv_message);
        ivContacts = findViewById(R.id.iv_contacts);
        ivFriends = findViewById(R.id.iv_friends);
        ivMe = findViewById(R.id.iv_me);

        tvMessage.setOnClickListener(this);
        tvContacts.setOnClickListener(this);
        tvFriends.setOnClickListener(this);
        tvMe.setOnClickListener(this);

        ivMessage.setOnClickListener(this);
        ivContacts.setOnClickListener(this);
        ivFriends.setOnClickListener(this);
        ivMe.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tv_message || id == R.id.iv_message) {
            setTabSelection(0);
        } else if (id == R.id.tv_contacts || id == R.id.iv_contacts) {
            setTabSelection(1);
        } else if (id == R.id.tv_friends || id == R.id.iv_friends) {
            setTabSelection(2);
        } else if (id == R.id.tv_me || id == R.id.iv_me) {
            setTabSelection(3);
        }
    }

    private void setTabSelection(int index) {
        setTabSelection(index, null);
    }

    private void setTabSelection(int index, String content) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        hideFragments(transaction);
        resetIcons();
        switch (index) {
            case 0:
                if (fg1 == null) {
                    if (content != null) {
                        fg1 = MyFragment.newInstance(content);
                    } else {
                        fg1 = MyFragment.newInstance("这是消息界面");
                    }
                    transaction.add(R.id.fragment_container, fg1);
                } else {
                    transaction.show(fg1);
                }
                ivMessage.setImageResource(R.drawable.txt_message);
                break;
            case 1:
                if (fg2 == null) {
                    fg2 = MyFragment.newInstance("这是通讯录界面");
                    transaction.add(R.id.fragment_container, fg2);
                } else {
                    transaction.show(fg2);
                }
                ivContacts.setImageResource(R.drawable.txt_friend);
                break;
            case 2:
                if (fg3 == null) {
                    fg3 = MyFragment.newInstance("这是朋友圈界面");
                    transaction.add(R.id.fragment_container, fg3);
                } else {
                    transaction.show(fg3);
                }
                ivFriends.setImageResource(R.drawable.txt_find);
                break;
            case 3:
                if (fg4 == null) {
                    fg4 = MyFragment.newInstance("这是我的界面");
                    transaction.add(R.id.fragment_container, fg4);
                } else {
                    transaction.show(fg4);
                }
                ivMe.setImageResource(R.drawable.txt_myself);
                break;
        }
        transaction.commit();
    }

    private void hideFragments(FragmentTransaction transaction) {
        if (fg1 != null) {
            transaction.hide(fg1);
        }
        if (fg2 != null) {
            transaction.hide(fg2);
        }
        if (fg3 != null) {
            transaction.hide(fg3);
        }
        if (fg4 != null) {
            transaction.hide(fg4);
        }
    }

    private void resetIcons() {
        ivMessage.setImageResource(R.drawable.xiaoxi);
        ivContacts.setImageResource(R.drawable.tongxunlu);
        ivFriends.setImageResource(R.drawable.pengyouquan);
        ivMe.setImageResource(R.drawable.xiaoren);
    }
}